---@class Battle.EffectBase
---@field New fun() : Battle.EffectBase
local EffectBase = Battle.CreateClass("EffectBase")
Battle.EffectBase = EffectBase

function EffectBase:ctor(_type)
    self.effectType = _type
end
function EffectBase:OnCreate()
    self.resName = ""
    self.id = 0
    self.loadRequestId = 0
    self.isPlaying = false
    ---@type GameObject
    self.obj = nil
    self.isDestroy = false
    self.isActive = true
    self.isLoading = false
    self.targetId = 0   --目标对象
    self.during = -1
    self.curTime = 0
    self.isDie = false
    self.pos = self.pos or Vector3.New()
    self.forward = self.forward or Vector3.New()
    self.scale = 1
end
function EffectBase:SetParam(resName,startPos,StartForward,during)
    self.resName = resName
    if startPos then
        self.pos:SetV(startPos)
    else
        self.pos:Set(0,0,0)
    end
    if StartForward then
        self.forward:SetV(StartForward)
        self.forward:SetNormalize()
    else
        self.forward:Set(0,0,1)
    end
    self.during = during or -1
    self.curTime = 0
    self.tranParent = Battle.SceneMgr.sceneRoot
end

function EffectBase:OnDestroy()
    self.isDestroy = true
    self.isDie = true
    Battle.PoolMgr.CancelLoad(self.loadRequestId)
    Battle.PoolMgr.ReturnPrefabToPool(self.resName,self.obj)
    self.targetId = 0
    self.loadRequestId = 0
    self.obj = nil
end
function EffectBase:Reset(uniqueId)
    self:OnCreate()
    self.id = uniqueId or 0
end
function EffectBase:IsDie()
    return self.isDie
end
function EffectBase:Die()
    self.isDie = true

end
function EffectBase:SetTranParent(parent)
    self.tranParent = parent
    if self.obj then
        Util.SetParent(self.obj,parent)
    end
end
function EffectBase:Execute()
    self.isPlaying = true
    if string.IsEmpty(self.resName) then
        return
    end
    self.curTime = 0
    self.isDie = false
    self:SetPosV(self.pos)
    self:SetObjForward(self.forward)

    if self.isLoading then return end
    self.isLoading = true
    self.loadRequestId = Battle.PoolMgr.LoadPrefabFromPool(self.resName,self.LoadCallback,self)
end
function EffectBase:LoadCallback(obj)
    if obj == nil then return end
    self.obj = obj
    self.obj.name = self.resName
    self:SetPosV(self.pos)
    self:SetObjForward(self.forward)
    self:SetTranParent(self.tranParent)
    self:SetScale(self.scale)
    self:SetActive(self.isActive)
    self:OnLoadFinished()
end
---@return Battle.Entity
function EffectBase:GetTarget()
    return Battle.ActorManager.GetActor(self.targetId)
end
function EffectBase:SetPos(x,y,z)
    self.pos:Set(x,y,z)
    if self.obj then
        Util.SetPosition(self.obj,x,y,z)
    end
end
function EffectBase:SetPosV(pos)
    self.pos:SetV(pos)
    if self.obj then
        Util.SetPosition(self.obj,pos.x,pos.y,pos.z)
    end
end
function EffectBase:SetScale(scale)
    self.scale = scale
    if self.obj then
        Util.SetLocalScale(self.obj,scale,scale,scale)
    end
end
function EffectBase:SetActive(isActive)
    if self.isActive == isActive then return end
    self.isActive = isActive
    if self.obj then
        self.obj:SetActive(isActive)
    end
end
function EffectBase:SetObjForward(forward)
    if self.obj then
        self.obj.transform.forward = forward
    end
end
function EffectBase:Update(dt)
    self.curTime = self.curTime + dt
    if self.during ~= -1 then
        if self.curTime >= self.during then
            self:Die()
            return
        end
    end
end

function EffectBase:OnLoadFinished()

end